package com.imooc.mall.controller;

import com.imooc.mall.common.ApiRestResponse;
import com.imooc.mall.filter.UserFilter;
import com.imooc.mall.model.vo.CartVO;
import com.imooc.mall.service.CartService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 购物车相关接口
 */
@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    CartService cartService;

    @ApiOperation("购物车列表")
    @GetMapping("/list")
    public ApiRestResponse list() {
        // 内部获取用户Id, 防止横向越权
        List<CartVO> cartlist = cartService.list(UserFilter.currentUser.getId());
        return ApiRestResponse.success(cartlist);
    }

    @ApiOperation("添加商品到购物车")
    @PostMapping("/add")
    public ApiRestResponse add(@RequestParam Integer productId, @RequestParam Integer count) {
        Integer userId = UserFilter.currentUser.getId();
        List<CartVO> cartVOList = cartService.add(userId, productId, count);
        return ApiRestResponse.success(cartVOList);
    }

    @ApiOperation("更新购物车")
    @PostMapping("/update")
    public ApiRestResponse update(@RequestParam Integer productId, @RequestParam Integer count) {
        Integer userId = UserFilter.currentUser.getId();
        List<CartVO> cartVOList = cartService.update(userId, productId, count);
        return ApiRestResponse.success(cartVOList);
    }

    @ApiOperation("删除购物车")
    @PostMapping("/delete")
    public ApiRestResponse delete(@RequestParam Integer productId) {
        // 不能传入userId，cartId，否则可以删除其他人的Id
        Integer userId = UserFilter.currentUser.getId();
        List<CartVO> cartVOList = cartService.delete(userId, productId);
        return ApiRestResponse.success(cartVOList);
    }

    @ApiOperation("选择/不选择购物车的某个商品")
    @PostMapping("/selete")
    public ApiRestResponse select(@RequestParam Integer productId, @RequestParam Integer selected) {
        // 不能传入userId，cartId，否则可以删除其他人的Id
        Integer userId = UserFilter.currentUser.getId();
        List<CartVO> cartVOList = cartService.selectOrNot(userId, productId, selected);
        return ApiRestResponse.success(cartVOList);
    }

    @ApiOperation("全选择/全不选择购物车的某个商品")
    @PostMapping("/seleteAll")
    public ApiRestResponse selectAll(@RequestParam Integer selected) {
        // 不能传入userId，cartId，否则可以删除其他人的Id
        Integer userId = UserFilter.currentUser.getId();
        List<CartVO> cartVOList = cartService.selectAllOrNot(userId, selected);
        return ApiRestResponse.success(cartVOList);
    }
}
